<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/3
 * Time: 23:33
 */
header('Content-type: application/json;charset=utf-8');
require_once(dirname(__FILE__) . '/../../inc/config.inc.php');
require_once(dirname(__FILE__) . '/../../function.php');
IsModelPriv('goodsorder');
$tbname = '#@__project';
//引入操作类
$field = isset($_POST['field']) ? $_POST['field'] : [];
$action = empty($_POST['action']) ? "" : htmlspecialchars($_POST['action']);

$wx_uid = $_SESSION['uid'];
$poster = "";
$op_uid = "";
$post_arr = $dosql->GetOne("select `truename` from `#@__user` where id='$wx_uid' ");
if (is_array($post_arr) && isset($post_arr['truename'])) {
    $poster = $post_arr['truename'];
}


if ($action == 'add') {
    if (count($field) == 0) {
        $jssdk->return_json_body(['code' => -1, 'msg' => '非法请求']);
    }

    $tasktype = empty($field['task_type']) ? 0 : intval($field['task_type']);
    $task = empty($field['task']) ? 0 : intval($field['task']);
    $crmid = isset($field['crmid']) ? intval($field['crmid']) : 0;
    $pmid = isset($field['pmid']) ? intval($field['pmid']) : 0;


    $area = empty($field['area']) ? "" : htmlspecialchars($field['area']);
    $projectname = empty($field['projectname']) ? "" : htmlspecialchars($field['projectname']);
    $school = empty($field['school']) ? "" : htmlspecialchars($field['school']);
    $truename = empty($field['truename']) ? "" : htmlspecialchars($field['truename']);
    $mobile = empty($field['mobile']) ? "" : htmlspecialchars($field['mobile']);
    $address = empty($field['address']) ? "" : htmlspecialchars($field['address']);
    $mode = empty($field['mode']) ? "q" : htmlspecialchars($field['mode']);
    $content = empty($field['content']) ? "" : htmlspecialchars($field['content']);
    $manageids = empty($field['manageids']) ? "" : htmlspecialchars($field['manageids']);
    $beginTime = empty($field['beginTime']) ? "" : htmlspecialchars($field['beginTime']);
    $endTime = empty($field['endTime']) ? "" : htmlspecialchars($field['endTime']);
    $oktime = empty($field['oktime']) ? "" : htmlspecialchars($field['oktime']);
    $oktime2 = empty($field['oktime2']) ? "" : htmlspecialchars($field['oktime2']);
    $picarr = isset($field['picarr_arr']) ? json_encode($field['picarr_arr']) : "";
    $picarr2 = isset($field['picarr_arr2']) ? json_encode($field['picarr_arr2']) : "";


    // 工单信息入库
    $apply_type = htmlspecialchars($tasktype);
    $apply_device = htmlspecialchars($task);
    $jindu = "3";
    $latitude = "33.006170";
    $longitude = "114.015880";
    $accuracy = "14";
    $post_time = time();
    $post_time2 = date("Y-m-d H:i:s", time());
    $beginTime = strtotime($beginTime);
    $endTime = strtotime($endTime);
    $oktime = $oktime == "" ? 0 : strtotime($oktime);
    $oktime2 = $oktime2 == "" ? 0 : strtotime($oktime2);


    $fee = "";
    //调取任务奖励
    if ($task > 0) {
        $fee_arr = $dosql->GetOne("SELECT * FROM `#@__task` WHERE id='$task'");
        if (is_array($fee_arr) && isset($fee_arr['id'])) {
            $fee = $fee_arr['value'];
        }
    }
    //调取 用户数据

    $crm_uid = "";  //用户的 uid
    $crm_openid_str = "";


    $remark_info = GetAuthor() . " 直接创建工程";


    $op_op_name = GetAuthor() . ' 添加工程';
    //新建工单
    $sql2 = "insert into `#@__project`(openid,uid,school,truename,mobile,address,apply_type,apply_device,projectname,num,content,pmid,manageid,begintime,endtime,post_time,jindu,latitude,longitude,accuracy,poster,remark,remark_time,fee,crmid,is_accept,accept_time,mode,picfiles,oktime,oktime2,planpicurls) values   ('$crm_openid_str','$crm_uid','$school','$truename','$mobile','$address','$apply_type','$apply_device','$projectname','1','$content','$pmid','','$beginTime','$endTime','$post_time','$jindu','$latitude','$longitude','$accuracy','$poster','$remark_info','$post_time','$fee','$crmid','1','$post_time','$mode','$picarr','$oktime','$oktime2','$picarr2')";
    $dosql->ExecNoneQuery($sql2);
    $orderid = $dosql->GetLastID();


    $dosql->ExecNoneQuery("insert into `#@__project_log`(orderid,op,op_time,jindu,manageid) values ('$orderid','$op_op_name','$post_time','2','$wx_uid')");


    //获取pm的 openid
    $m = $dosql->GetOne("select openid  from `#@__user`  where id='$pmid'");
    $openid_str = $m['openid'];
    //插入更新的状态

    $dosql->ExecNoneQuery("insert into `#@__project_log`(orderid,op,op_time,jindu,manageid) values ('$orderid','系统派单给项目经理','$post_time','3','$pmid')");


    $orderidname = date('Ymd', time()) . "-" . $orderid;
    //封装为json格式的数据
    $repairname = getRepairMode($apply_device);

    $repairname = $repairname . "  奖励  " . $fee;


    $send_body['touser'] = $openid_str;
    $send_body['template_id'] = $cfg_neworder;
    $send_body['url'] = $cfg_weburl . 'wx/projectDetail.php?projectid=' . $orderid;
    $send_body['data']['first']['value'] = "📢 您好,系统下发新的工程需要您处理！";
    $send_body['data']['first']['color'] = "#FF0000";
    $send_body['data']['keyword1']['value'] = $orderidname;
    $send_body['data']['keyword1']['color'] = "#173177";
    $send_body['data']['keyword2']['value'] = $repairname;
    $send_body['data']['keyword2']['color'] = "#FF0000";
    $send_body['data']['keyword3']['value'] = $post_time2;
    //$send_body['data']['keyword3']['color'] = "#FF0000";
    $send_body['data']['keyword4']['value'] = "💗" . $school . "-" . $truename;
    $send_body['data']['keyword4']['color'] = "#0e48cb";
    $send_body['data']['keyword5']['value'] = "⛳" . $address;
    $send_body['data']['keyword5']['color'] = "#0e48cb";
    $send_body['data']['remark']['value'] = "👉请点击，选择工程服务人员。服务热线:" . $cfg_hotline . "👈";
    $send_body['data']['remark']['color'] = "#66bf45";

    $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $g_AccessToken;
    $jssdk->https_curl_json($url, $send_body, 'json');
    $jssdk->return_json_body(['code' => 0, 'msg' => '添加成功']);


}


if ($action == "update") {
    if (count($field) == 0) {
        $jssdk->return_json_body(['code' => -1, 'msg' => '非法请求']);
    }


    $id = isset($field['id']) ? intval($field['id']) : 0;
    $orderid = $id;
    $tasktype = empty($field['task_type']) ? 0 : intval($field['task_type']);
    $task = empty($field['task']) ? 0 : intval($field['task']);
    $crmid = isset($field['crmid']) ? intval($field['crmid']) : 0;
    $pmid = isset($field['pmid']) ? intval($field['pmid']) : 0;


    $area = empty($field['area']) ? "" : htmlspecialchars($field['area']);

    $projectname = empty($field['projectname']) ? "" : htmlspecialchars($field['projectname']);
    $school = empty($field['school']) ? "" : htmlspecialchars($field['school']);
    $truename = empty($field['truename']) ? "" : htmlspecialchars($field['truename']);
    $mobile = empty($field['mobile']) ? "" : htmlspecialchars($field['mobile']);
    $address = empty($field['address']) ? "" : htmlspecialchars($field['address']);
    $mode = empty($field['mode']) ? "q" : htmlspecialchars($field['mode']);
    $content = empty($field['content']) ? "" : htmlspecialchars($field['content']);
    $manageids = empty($field['manageids']) ? "" : htmlspecialchars($field['manageids']);
    $beginTime = empty($field['beginTime']) ? "" : htmlspecialchars($field['beginTime']);
    $endTime = empty($field['endTime']) ? "" : htmlspecialchars($field['endTime']);
    $oktime = empty($field['oktime']) ? "" : htmlspecialchars($field['oktime']);
    $oktime2 = empty($field['oktime2']) ? "" : htmlspecialchars($field['oktime2']);
    $picarr = isset($field['picarr_arr']) ? json_encode($field['picarr_arr']) : "";
    $picarr2 = isset($field['picarr_arr2']) ? json_encode($field['picarr_arr2']) : "";


    // 工单信息入库
    $apply_type = htmlspecialchars($tasktype);
    $apply_device = htmlspecialchars($task);
    $jindu = "3";
    $latitude = "33.006170";
    $longitude = "114.015880";
    $accuracy = "14";
    $post_time = time();
    $post_time2 = date("Y-m-d H:i:s", time());
    $beginTime = strtotime($beginTime);
    $endTime = strtotime($endTime);
    $oktime = strtotime($oktime);
    $oktime2 = strtotime($oktime2)+86399;


    $fee = "";
    //调取任务奖励
    if ($task > 0) {
        $fee_arr = $dosql->GetOne("SELECT * FROM `#@__task` WHERE id='$task'");
        if (is_array($fee_arr) && isset($fee_arr['id'])) {
            $fee = $fee_arr['value'];
        }
    }
    //调取 用户数据

    $crm_uid = "";  //用户的 uid
    $crm_openid_str = "";


    $remark_info = GetAuthor() . " 修改工程";


    $op_op_name = GetAuthor() . ' 修改工程';
    //新建工单
    $sql2 = "update `$tbname`  set projectname='$projectname',apply_device='$apply_device',school='$school',truename='$truename',mobile='$mobile',address='$address',begintime='$beginTime',endtime='$endTime',content='$content',picfiles='$picarr',planpicurls='$picarr2',pmid='$pmid',oktime='$oktime',oktime2='$oktime2',area='$area' where id='$id' ";
    $dosql->ExecNoneQuery($sql2);


    $dosql->ExecNoneQuery("insert into `#@__project_log`(orderid,op,op_time,jindu,manageid) values ('$orderid','$op_op_name','$post_time','2','$wx_uid')");


    //获取pm的 openid
    $m = $dosql->GetOne("select openid  from `#@__user`  where id='$pmid'");
    $openid_str = $m['openid'];
    //插入更新的状态

    $dosql->ExecNoneQuery("insert into `#@__project_log`(orderid,op,op_time,jindu,manageid) values ('$orderid','系统发送信息给项目经理','$post_time','3','$pmid')");


    $orderidname = date('Ymd', time()) . "-" . $orderid;
    //封装为json格式的数据
    $repairname = getRepairMode($apply_device);

    $repairname = $repairname . "  奖励  " . $fee;


    $send_body['touser'] = $openid_str;
    $send_body['template_id'] = $cfg_neworder;
    $send_body['url'] = $cfg_weburl . 'wx/projectDetail.php?projectid=' . $orderid;
    $send_body['data']['first']['value'] = "📢 您好,管理员修改了工程信息，请知悉！";
    $send_body['data']['first']['color'] = "#FF0000";
    $send_body['data']['keyword1']['value'] = $orderidname;
    $send_body['data']['keyword1']['color'] = "#173177";
    $send_body['data']['keyword2']['value'] = $repairname;
    $send_body['data']['keyword2']['color'] = "#FF0000";
    $send_body['data']['keyword3']['value'] = $post_time2;
    //$send_body['data']['keyword3']['color'] = "#FF0000";
    $send_body['data']['keyword4']['value'] = "💗" . $school . "-" . $truename;
    $send_body['data']['keyword4']['color'] = "#0e48cb";
    $send_body['data']['keyword5']['value'] = "⛳" . $address;
    $send_body['data']['keyword5']['color'] = "#0e48cb";
    $send_body['data']['remark']['value'] = "👉请点击，选择工程服务人员。服务热线:" . $cfg_hotline . "👈";
    $send_body['data']['remark']['color'] = "#66bf45";

    $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $g_AccessToken;
    $jssdk->https_curl_json($url, $send_body, 'json');
    $jssdk->return_json_body(['code' => 0, 'msg' => '编辑成功']);

}


if ($action == 'del') {
    $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
    $dosql->ExecNoneQuery("UPDATE $tbname  SET is_delete='1',deltime='$time' WHERE id=$id ");
    $row_arr['code'] = 0;
    $row_arr['msg'] = "删除成功";
    echo json_encode($row_arr);
}
if ($action == 'success') {
    $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
   

    $time=time();
    //修改订单状态
    $dosql->ExecNoneQuery("update  `#@__project` set jindu='7',checktime='$time' where id='$id' ");
    //记录工单操作
    $dosql->ExecNoneQuery("insert into `#@__project_log`(orderid,op,op_time,jindu,remark) values ('$id','工程验收通过','$time','7','管理员后台操作')");



    $row_arr['code'] = 0;
    $row_arr['msg'] = "操作成功";
    echo json_encode($row_arr);
}
